package com.zhang.test_zhang.service.impl;

import com.zhang.test_zhang.entity.Order;
import com.zhang.test_zhang.entity.User;
import com.zhang.test_zhang.mapper.OrderMapper;
import com.zhang.test_zhang.mapper.ShoppingCartMapper;
import com.zhang.test_zhang.mapper.UserMapper;
import com.zhang.test_zhang.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 支付100元
     *
     * @param
     * @return
     */
    @Override
    @Transactional //开启事务
    public String pay(Order order) {

        int id = UUID.randomUUID().toString().replaceAll("-", "").hashCode();
        id = id < 0 ? -id : id;
        int orderNumber = UUID.randomUUID().toString().replaceAll("-", "").hashCode();
        orderNumber = id < 0 ? -id : id;

        //获取用户id
        Integer userId = order.getUserId();
        //支付的金额
        BigDecimal orderMoney = BigDecimal.valueOf(100);

        order.setId(id);
        order.setOrderNumber(orderNumber);
        order.setOrderMoney(orderMoney);
        //账单类型
        order.setType(1);
        //根据用户id查询用户
        User user = userMapper.selectById(userId);
        //获取订单的金额
        BigDecimal wallet = user.getWallet();
        if (wallet.compareTo(orderMoney) > -1){
            //向订单表插入数据
            orderMapper.pay(order);
            //用户的金额减去订单的金额
            BigDecimal sum = wallet.subtract(orderMoney);
            user.setWallet(sum);
            user.setId(userId);
            //修改用户金额
            userMapper.updateById(user);
            return "支付成功！";
        }else {
            return "余额不足,无法支付！";
        }
    }

    /**
     * 用户定单id退款
     * @param order
     * @return
     */
    @Override
    @Transactional //添加事务
    public String drawback(Order order) {

        Integer id = order.getId();
        order.setType(2);

        //根据订单id查询订单数据
        Order selectById = orderMapper.selectById(id);
        if (selectById == null){
            return "没有查到该订单";
        }
        Integer type = selectById.getType();
        if (type == 2){
            return "该订单不是支付订单，无法申请退款";
        }
        Integer userId = selectById.getUserId();
        BigDecimal orderMoney = selectById.getOrderMoney();

        //更新用户钱包
        User user = new User();
        user.setId(userId);
        //根据用户id查询用户
        User userById = userMapper.selectById(userId);
        BigDecimal wallet = userById.getWallet();
        //用户钱包金额加退款金额
        BigDecimal add = wallet.add(orderMoney);
        user.setWallet(add);
        userMapper.updateById(user);
        //修改账单单数据
        orderMapper.update(order);

        return "退款成功！";
    }

    /**
     * 查询用户消费明细
     * @param userId
     * @return
     */
    @Override
    public List<Order> getAll(int userId) {
        List<Order> orderList = orderMapper.getAll(userId);
        return orderList;
    }
}
